<?php

namespace app\admin\model;

use app\common\model\BaseModel;


class UserRefund extends BaseModel
{

    

    

    // 表名
    protected $name = 'user_refund';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text',
        'type_text',
        'payment_time_text',
        'user_info_text'
    ];
    

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2'), '3' => __('Status 3'), '4' => __('Status 4')];
    }

    public function getTypeList()
    {
        return ['0' => __('Type 0'), '1' => __('Type 1'), '2' => __('Type 2')];
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getPaymentTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['payment_time']) ? $data['payment_time'] : '');
        return is_numeric($value) ? date("Y-m-d", $value) : $value;
    }

    protected function setPaymentTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    public function getUserInfoTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['user_id']) ? $data['user_id'] : '');
        if(!$value){
            return '';
        }
        $userinfo=User::where('id',$value)->field('username,mobile')->find();
        return isset($userinfo) ? $userinfo : '';
    }
    /**
     * 查询多条数据（分页）
     *
     * @param array $where
     * @param integer $pagesize
     * @param string $orderby
     * @param string $field
     * @return void
     */
    public function getSelect(array $where,$pagesize=20,$orderby='id desc',$field='*'){
        return $this->where($where)->order($orderby)->field($field)->paginate($pagesize);
    }
    /**
     * 添加数据
     *
     * @param [type] $user_id
     * @param [type] $money
     * @param [type] $bank_card
     * @param [type] $bank_name
     * @param [type] $user_name
     * @param [type] $phone
     * @param [type] $sub_branch
     * @param string $type
     * @return void
     */
    public function setCreate(int $user_id,$money,$bank_card,$bank_name,$user_name,$phone,$sub_branch,$type='0'){
        $arr=[
            'user_id'=>$user_id,
            'real_fee'=>$money,
            'bank_card'=>$bank_card,
            'bank_name'=>$bank_name,
            'user_name'=>$user_name,
            'phone'=>$phone,
            'sub_branch'=>$sub_branch,
            'status'=>'0',
            'type'=>$type,
            'remake'=>$user_name.'提现：'.$money,
            'createtime'=>time()
        ];
        $data=$this->create($arr);
        if($data){
            return ['res'=>true,'data'=>$data];
        }else{
            return ['res'=>false];
        }
    }
}
